(function ($) {
	var socket;
	var popNum = ""; //呼入号码，用来使同一号码在两个连续的事件中不重复弹屏
	var timestamp;   //呼入号码时间戳，同一电话规定时间外再次打入可以弹屏

	var timer = null;
	
	$.openWebSocket = function (url){
		if (!window.WebSocket) {
		  window.WebSocket = window.MozWebSocket;
		}
		if (window.WebSocket) {
			socket = new WebSocket(url);
			socket.onmessage = function(event) {			      
				   $.eventFilter(event)				  				
			};
			socket.onopen = function(event) { 
				webSocketOnOpen(event);
			};
			socket.onclose = function(event) { 
				webSocketOnClose(event);
			};
		} else {
			alert("您的浏览器不支持WebSocket,请更换浏览器！");
		}
	}
	
	/**
	 * Event 过滤
	 */
	$.eventFilter = function (event) {
		var bl = true;
		var obj = JSON.parse(event.data);
		if(obj.Event == 'ScreenPopup'){
		  if(popNum != obj.InLineNum){
			  popNum = obj.InLineNum;
              timestamp = Date.parse(new Date());			  
			}else{
				var nowTime = Date.parse(new Date());
				if(nowTime - timestamp < 5000){
				   bl = false;
				}
				
			}			
		}
		if(bl){webSocketOnMessage(event)};
	}
	
	/**
	  *	WebSocket发送
	  */
	$.send = function (message) {
		if("" == message){
			alert("websocket发送信息不能为空!");
			return false;
		}
		if (!window.WebSocket) { 
			return; 
		}
		if (socket.readyState == WebSocket.OPEN) {
			socket.send(message);
		} else {
			alert("连接已断开，请从新登陆，如有问题请联系管理员！");
		}
	}
	
	/**
	 *  WebSocket 心跳检测
	 *  extenNum  分机号 
	 */
	$.pingWebSocket = function (extenNum){
		if("" == extenNum){
			alert("心跳检测分机号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "PingWebSocket";
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
		setTimeout("$.pingWebSocket("+ extenNum +")", 30000);
	}
	
	/**
	  *	绑定分机,工号
	  * extenNum  分机号 
	  * queueName 队列号
	  * workNum   工号
	  */
	$.bindExtenNumAndWeb = function (extenNum,workerId){
		if("" == extenNum){
			alert("分机号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "BindExtenNumAndWeb";
		obj.ExtenNum = String(extenNum);
		obj.WorkerId = String(workerId);
		$.send($.toJSON(obj));
	}
	
	/**
	 * 解除绑定
	 * extenNum  分机号
	 */
	$.unBindExtenNumAndWeb = function (extenNum,queueName){
		if("" == extenNum){
			alert("分机号不能为空!");
			return false;
		}
		if("" == queueName){
			alert("队列号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "UnBindExtenNumAndWeb";
		obj.ExtenNum = String(extenNum);
		obj.QueueName = String(queueName);
		$.send($.toJSON(obj));
	}
	
	/**
	  *	查看分机状态
	  * numStr 要查看的分机号码，例："8888,9999"
	  */
	$.showPeers = function (numsStr){
		if("" == numsStr){
			alert("分机号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "ShowPeers";
		obj.NumsStr = String(numsStr);
		$.send($.toJSON(obj));
	}
	
	/**
	 * 查看所有队列号码
	 */
	$.showQueueNum = function (){
		var obj = new Object();
		obj.Action = "QueueNum";
		$.send($.toJSON(obj));
	}
	
	/**
	  *	呼叫
	  * extenNum 主叫号码
	  * callNum  被叫号码 
	  */
	$.dail = function (extenNum,callNum){
		if("" == extenNum){
			alert("主叫号不能为空!");
			return false;
		}
		if("" == callNum){
			alert("被叫号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "Dail";
		obj.ExtenNum = String(extenNum);
		obj.CallNum = String(callNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 挂断 
	  * extenNum 分机号码 
	  */
	$.hangUp = function (extenNum){
		if("" == extenNum){
			alert("分机号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "HangUp";
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 协商转接
	  * extenNum 主叫号码
	  * callNum  被叫号码 
	  */
	$.atxfer = function (extenNum,callNum){
		if("" == extenNum){
			alert("主叫号不能为空!");
			return false;
		}
		if("" == callNum){
			alert("被叫号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "Atxfer";
		obj.ExtenNum = String(extenNum);
		obj.CallNum = String(callNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 盲转 
	  * extenNum 主叫号码
	  * callNum  被叫号码
	  */
	$.redirect = function (extenNum,callNum){
		if("" == extenNum){
			alert("主叫号不能为空!");
			return false;
		}
		if("" == callNum){
			alert("被叫号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "Redirect";
		obj.ExtenNum = String(extenNum);
		obj.CallNum = String(callNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 查看指定/所有队列信息
	  * queueName 队列号码 
	  */
	$.queueStatus = function (queueName){
		if("" == queueName){
			alert("队列号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "QueueStatus";
		obj.QueueName = String(queueName);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 添加队列成员 
	  * queueName 队列号码
	  * extenNum  分机号码
	  */
	$.queueAdd = function (queueName ,extenNum){
		if("" == queueName){
			alert("队列号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("分机号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "QueueAdd";
		obj.QueueName = String(queueName);
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 删除队列成员
	  * queueName 队列号码
	  * extenNum  分机号码
	  */
	$.queueRemove = function (queueName,extenNum){
		if("" == queueName){
			alert("队列号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("分机号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "QueueRemove";
		obj.QueueName = String(queueName);
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 队列成员，置闲/置忙 
	  * queueName 队列号码
	  * extenNum  分机号码
	  * paused    true忙/false闲 
	  */
	$.queuePause = function (queueName,extenNum,paused){
		if("" == queueName){
			alert("队列号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("分机号不能为空!");
			return false;
		}
		if("" == paused){
			alert("置闲/置忙状态不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "QueuePause";
		obj.QueueName = String(queueName);
		obj.ExtenNum = String(extenNum);
		obj.Paused = String(paused);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 查询所有正在进行的会议
	  */
	$.confbridgeListRooms = function(){
		var obj = new Object();
		obj.Action = "ConfbridgeListRooms";
		$.send($.toJSON(obj));
	}
	
	/**
	  * 查询指定会议中所有成员
	  * confName 会议号码
	  */
	$.confbridgeList = function(confName){
		if("" == confName){
			alert("会议号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "ConfbridgeList";
		obj.ConfName = String(confName);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 创建会议
	  * confName 会议号码
	  * extenNums 会议成员号码（例：8888）
	  */
	$.createConference = function(confName,extenNums){
		if("" == confName){
			alert("会议号不能为空!");
			return false;
		}
		if("" == extenNums){
			alert("会议成员号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "CreateConference";
		obj.ConfName = String(confName);
		obj.ExtenNum = String(extenNums);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 加入会议
	  * confName 会议号码
	  * extenNum 会议成员号码（例：9999）
	  */
	$.addConference =  function(confName,extenNum){
		if("" == confName){
			alert("会议号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("会议成员号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "AddConference";
		obj.ConfName = String(confName);
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 踢出会议中指定成员
	  * confName 会议号码
	  * extenNum 会议成员号码（例：8888）
	  */
	$.conferenceKick = function(confName,extenNum){
		if("" == confName){
			alert("会议号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("会议成员号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "ConferenceKick";
		obj.ConfName = String(confName);
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 锁住指定会议
	  * confName 会议号码
	  * extenNum 分机号码（例：8888）
	  */
	$.conferenceLock = function(confName,extenNum){
		if("" == confName){
			alert("会议号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("会议成员号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "ConferenceLock";
		obj.ConfName = String(confName);
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 解锁指定会议
	  * confName 会议号码
	  * extenNum 分机号码（例：8888）
	  */
	$.conferenceUnlock = function(confName,extenNum){
		if("" == confName){
			alert("会议号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("会议成员号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "ConferenceUnlock";
		obj.ConfName = String(confName);
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 禁言指定会议中指定成员
	  * confName 会议号码
	  * extenNums 会议成员号码（例：8888）
	  */
	$.conferenceMute = function(confName,extenNum){
		if("" == confName){
			alert("会议号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("会议成员号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "ConferenceMute";
		obj.ConfName = String(confName);
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 解除指定会议中指定成员的禁言
	  * confName 会议号码
	  * extenNums 会议成员号码（例：8888）
	  */
	$.conferenceUnmute = function(confName,extenNum){
		if("" == confName){
			alert("会议号不能为空!");
			return false;
		}
		if("" == extenNum){
			alert("会议成员号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "ConferenceUnmute";
		obj.ConfName = String(confName);
		obj.ExtenNum = String(extenNum);
		$.send($.toJSON(obj));
	}
	
	/**
	  * 监听
	  * extenNum 当前分机号码
	  * spyExten 被监听分机号
	  */
	$.chanSpy = function(extenNum,spyExten){
		if("" == extenNum){
			alert("会议号不能为空!");
			return false;
		}
		if("" == spyExten){
			alert("被监听分机号不能为空!");
			return false;
		}
		var obj = new Object();
		obj.Action = "ChanSpy";
		obj.ExtenNum = String(extenNum);
		obj.SpyExten = String(spyExten);
		$.send($.toJSON(obj));
	}


}(jQuery));
